//
//  RankHeaderView.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/17.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "RankHeaderView.h"

@interface RankHeaderView ()

@property (nonatomic, strong)UIButton *dayBtn;
@property (nonatomic, strong)UIButton *weekBtn;
@property (nonatomic, strong)UIButton *monthBtn;

@end

@implementation RankHeaderView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    [self addSubview:self.libraryBtn];
    [self addSubview:self.listenBtn];
    [self addSubview:self.dayBtn];
    [self addSubview:self.weekBtn];
    [self addSubview:self.monthBtn];
}

#pragma mark - event response
//图书排行榜
- (void)libraryBtnClickAction {
    if (!_libraryBtn.selected) {
        _libraryBtn.selected = YES;
        _listenBtn.selected = NO;
    }
}
//听书排行榜
- (void)listenBtnClickAction {
    if (!_listenBtn.selected) {
        _listenBtn.selected = YES;
        _libraryBtn.selected = NO;
    }
}
//日排行榜
- (void)dayBtnClickAction {
    if (!_dayBtn.selected) {
        _dayBtn.selected = YES;
        _weekBtn.selected = NO;
        _monthBtn.selected = NO;
    }
}
//周排行
- (void)weekBtnClickAction {
    if (!_weekBtn.selected) {
        _weekBtn.selected = YES;
        _dayBtn.selected = NO;
        _monthBtn.selected = NO;
    }
}
//月排行
- (void)monthBtnClickAction {
    if (!_monthBtn.selected) {
        _monthBtn.selected = YES;
        _dayBtn.selected = NO;
        _weekBtn.selected = NO;
    }
}

#pragma mark - 懒加载
- (UIButton *)libraryBtn {
    if (!_libraryBtn) {
        _libraryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _libraryBtn.frame = CGRectMake(0, 0, kScreenWidth / 2, 53);
        [_libraryBtn setTitle:@"图书排行榜" forState:UIControlStateNormal];
        _libraryBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_libraryBtn setTitleColor:MainDarkGrayColor forState:UIControlStateNormal];
        [_libraryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_libraryBtn setBackgroundColor:[UIColor colorFromHexCode:@"#e6e6e6"] forState:UIControlStateNormal];
        [_libraryBtn setBackgroundColor:MainGreenColor forState:UIControlStateSelected];
        _libraryBtn.selected = YES;
        [_libraryBtn addTarget:self action:@selector(libraryBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _libraryBtn;
}

- (UIButton *)listenBtn {
    if (!_listenBtn) {
        _listenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _listenBtn.frame = CGRectMake(kScreenWidth / 2, 0, kScreenWidth / 2, 53);
        [_listenBtn setTitle:@"听书排行榜" forState:UIControlStateNormal];
        _listenBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_listenBtn setTitleColor:MainDarkGrayColor forState:UIControlStateNormal];
        [_listenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_listenBtn setBackgroundColor:[UIColor colorFromHexCode:@"#e6e6e6"] forState:UIControlStateNormal];
        [_listenBtn setBackgroundColor:MainGreenColor forState:UIControlStateSelected];
        [_listenBtn addTarget:self action:@selector(listenBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _listenBtn;
}

- (UISegmentedControl *)segmentControl {
    if (!_segmentControl) {
        _segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"日排行",@"周排行",@"月排行"]];
        _segmentControl.frame = CGRectMake(20, self.listenBtn.bottom + 20, kScreenWidth - 40, 30);
        _segmentControl.selectedSegmentIndex = 0;
        _segmentControl.tintColor = MainGreenColor;
    }
    return _segmentControl;
}

- (UIButton *)dayBtn {
    if (!_dayBtn) {
        _dayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _dayBtn.frame = CGRectMake(20, self.listenBtn.bottom + 20, (kScreenWidth - 40) / 3, 30);
        [_dayBtn setTitle:@"日排行" forState:UIControlStateNormal];
        [_dayBtn setTitleColor:[UIColor colorFromHexCode:@"#000000"] forState:UIControlStateNormal];
        [_dayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        _dayBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_dayBtn setBackgroundColor:[UIColor colorFromHexCode:@"#e6e6e6"] forState:UIControlStateNormal];
        [_dayBtn setBackgroundColor:MainGreenColor forState:UIControlStateSelected];
        _dayBtn.selected = YES;
        [_dayBtn setLeftSideRadius];
        [_dayBtn addTarget:self action:@selector(dayBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dayBtn;
}

- (UIButton *)weekBtn {
    if (!_weekBtn) {
        _weekBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _weekBtn.frame = CGRectMake(20 + (kScreenWidth - 40) / 3, self.listenBtn.bottom + 20, (kScreenWidth - 40) / 3, 30);
        [_weekBtn setTitle:@"周排行" forState:UIControlStateNormal];
        [_weekBtn setTitleColor:[UIColor colorFromHexCode:@"#000000"] forState:UIControlStateNormal];
        [_weekBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        _weekBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_weekBtn setBackgroundColor:[UIColor colorFromHexCode:@"#e6e6e6"] forState:UIControlStateNormal];
        [_weekBtn setBackgroundColor:MainGreenColor forState:UIControlStateSelected];
        [_weekBtn addTarget:self action:@selector(weekBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _weekBtn;
}

- (UIButton *)monthBtn {
    if (!_monthBtn) {
        _monthBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _monthBtn.frame = CGRectMake(20 + (kScreenWidth - 40) / 3 * 2, self.listenBtn.bottom + 20, (kScreenWidth - 40) / 3, 30);
        [_monthBtn setTitle:@"月排行" forState:UIControlStateNormal];
        [_monthBtn setTitleColor:[UIColor colorFromHexCode:@"#000000"] forState:UIControlStateNormal];
        [_monthBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        _monthBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_monthBtn setBackgroundColor:[UIColor colorFromHexCode:@"#e6e6e6"] forState:UIControlStateNormal];
        [_monthBtn setBackgroundColor:MainGreenColor forState:UIControlStateSelected];
        [_monthBtn setRightSideRadius];
        [_monthBtn addTarget:self action:@selector(monthBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _monthBtn;
}

@end
